﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Threading;
using System.Xml.Serialization;

using P2PLib.Client.DataModel;

namespace P2PLib.Client.Utils
{
    public class SharedFileLibraryUtils
    {
        
        /// <summary>
        /// 从指定的路径中加载共享文件信息：
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static SharedFileLibrary  LoadSharedFileLibrary(string path)
        {
            SharedFileLibrary fl=new SharedFileLibrary();
            try
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                XmlSerializer xs = new XmlSerializer(typeof(SharedFileLibrary));
                fl = (SharedFileLibrary)xs.Deserialize(fs);
            }
            catch (Exception e)
            {
            }
            return fl;
        }

       /// <summary>
       /// 保存共享文件信息：
       /// </summary>
       /// <param name="dir"></param>
       /// <param name="fl"></param>
        public static void SaveFileList(string path,SharedFileLibrary sfl)
        {
                FileInfo f = new FileInfo(path);
                if (f.Exists)
                {
                    f.Delete();
                }
                FileStream fs=new FileStream(path,FileMode.OpenOrCreate);

                XmlSerializer xs = new XmlSerializer(typeof(SharedFileLibrary));
                xs.Serialize(fs,sfl);

        }

        /// <summary>
        /// 获得指定文件的HashId
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileID(string path)
        {
            FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read,8192);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] code=md5.ComputeHash(fs);
            return BitConverter.ToString(code);
        }

        /// <summary>
        /// 获得给定的目录以及子目录下的所有文件的文件信息
        /// </summary>
        /// <param name="dirs"></param>
        /// <returns></returns>
        public static  List<LocalFileInfo> GetLocalFileInfo(DirectoryInfo[] dirs)
        {
            List<LocalFileInfo> fileInfo=new List<LocalFileInfo>();

            foreach (DirectoryInfo dir in dirs)
            {
                FileInfo[] files = dir.GetFiles();
                foreach(FileInfo f in files)
                {
                    Console.WriteLine("Processing {0}", f.FullName);

                    CFileInfo cFile=new CFileInfo();
                    cFile.FileHashCode=GetFileID(f.FullName);
                    cFile.FileName=f.Name;
                    cFile.FileSize=f.Length;
                    fileInfo.Add(new LocalFileInfo(cFile,f.FullName,f.LastWriteTime));
                }

                List<LocalFileInfo> subFiles=GetLocalFileInfo(dir.GetDirectories());

                foreach(LocalFileInfo lf in subFiles)
                    fileInfo.Add(lf);

            }

            return fileInfo;
        }

        public static SharedFileLibrary GenerateFileList(DirectoryInfo[] dirs)
        {
            SharedFileLibrary sfl = new SharedFileLibrary();
            sfl.Files = GetLocalFileInfo(dirs);
            List<string> strs = new List<string>();
            foreach (DirectoryInfo dir in dirs)
            {
                strs.Add(dir.FullName);
            }

            sfl.Dirs = strs;
            return sfl;
        }


    }
}
